package com.stadium.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.aliyuncs.ram.model.v20150501.GetAccountAliasRequest;
import com.netflix.discovery.converters.Auto;
import com.stadium.common.exception.RRException;
import com.stadium.common.utils.Md5Utils;
import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.StringUtils;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.*;
import com.stadium.entity.GamePlayer;
import com.stadium.entity.RTeamUser;
import com.stadium.entity.SysUser;
import com.stadium.entity.UserMessage;
import com.stadium.mapper.GamePlayerMP;
import com.stadium.mapper.GameTeamMP;
import com.stadium.mapper.RTeamUserMP;
import com.stadium.mapper.SysUserMP;
import com.stadium.service.RTeamUserSV;
import com.stadium.service.SysUserSV;
import com.stadium.service.UserMessageSV;
import com.stadium.vo.RTeamUserVo;
import com.stadium.vo.SysUserVo;
import com.stadium.vo.UserMessageVo;
import com.stadium.vo.searchVo.RTeamUserSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RTeamUserSVI implements RTeamUserSV {
    @Autowired
    private RTeamUserMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private SysUserSV sysUserSV;
    @Autowired
    private SysUserMP sysUserMP;
    @Autowired
    private GamePlayerMP gamePlayerMP;
    @Autowired
    private UserMessageSV userMessageSV;
    @Autowired
    private GameTeamMP gameTeamMP;

    @Override
    public List<RTeamUser> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, RTeamUser.class);
    }

    @Override
    @Transactional
    public void save(RTeamUserVo rTeamUserVo) {
        //保存球员
        GamePlayer gamePlayer = new GamePlayer();
        BeanUtils.copyProperties(rTeamUserVo, gamePlayer);
        SysUserDto sysUserDto =sysUserSV.getUserByIdNumber(rTeamUserVo.getIdNumber());
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(rTeamUserVo, sysUser);
        sysUser.setCreateUser(UserUtils.getUserId());
        sysUser.setUpdateUser(UserUtils.getUserId());
        if (sysUserDto == null) {
            String userid = UUID.randomUUID().toString().replaceAll("-", "");
            sysUser.setId(userid);
            sysUser.setPassword(Md5Utils.hash("000000"));
            sysUserMP.save(sysUser);
            gamePlayer.setUserId(userid);
        }else {
            sysUser.setId(sysUserDto.getId());
            sysUserMP.update(sysUser);
            gamePlayer.setUserId(sysUserDto.getId());
        }
        GamePlayerDto gamePlayerDto = gamePlayerMP.getByUserId(gamePlayer.getUserId());
        if (gamePlayerDto == null) {
            String playerid = UUID.randomUUID().toString().replaceAll("-", "");
            gamePlayer.setId(playerid);
            gamePlayer.setCreateUser(UserUtils.getUserId());
            gamePlayer.setUpdateUser(UserUtils.getUserId());
            gamePlayerMP.save(gamePlayer);
        }else {
            gamePlayer.setId(gamePlayerDto.getId());
            gamePlayerMP.update(gamePlayer);
        }
        if (rTeamUserVo.getTeamId() != null && !rTeamUserVo.getTeamId().isEmpty()) {
            RTeamUser rTeamUser1 = mapper.getbyTeamIdAndUserId(rTeamUserVo.getTeamId(), gamePlayer.getUserId());
            if (rTeamUser1 == null) {
                RTeamUser rTeamUser = new RTeamUser();
                BeanUtils.copyProperties(rTeamUserVo, rTeamUser);
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                rTeamUser.setId(id);
                rTeamUser.setUserId(gamePlayer.getUserId());
                rTeamUser.setTeamId(rTeamUserVo.getTeamId());
                rTeamUser.setAuditStatus("1");
                mapper.save(rTeamUser);
            }
        }

    }

    @Override
    @Transactional
    public void update(RTeamUserVo rTeamUserVo) {
        SysUserDto sysUserDto = sysUserMP.get(rTeamUserVo.getId());
        SysUser sysUser  =new SysUser();
        sysUser.setId(sysUserDto.getId());
        BeanUtils.copyProperties(rTeamUserVo,sysUser);
        sysUserMP.update(sysUser);

        GamePlayerDto gamePlayerDto = gamePlayerMP.getByUserId(sysUserDto.getId());
        GamePlayer gamePlayer = new GamePlayer();
        BeanUtils.copyProperties(rTeamUserVo, gamePlayer);
        gamePlayer.setId(gamePlayerDto.getId());
        gamePlayerMP.update(gamePlayer);


        /*RTeamUser rTeamUser = new RTeamUser();
        BeanUtils.copyProperties(rTeamUserVo, rTeamUser);
        //rTeamUser.setUpdateUser(UserUtils.getUserId());
        mapper.update(rTeamUser);*/
    }

    @Override
    public void saveBatch(List<RTeamUser> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    public List<RTeamUserDto> query(RTeamUserSearchVo searchVo) {
        return mapper.query(searchVo);
    }

    @Override
    public RTeamUserDto get(String id) {
        return mapper.get(id);
    }

    @Override
    public int getCount(RTeamUserSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

    @Override
    public List<SysUserDto1> getUsersByTeamId(String teamId) {
        return mapper.getUsersByTeamId(teamId);
    }

    @Override
    public void apply(String teamId, String userId) {

        RTeamUser user = mapper.getbyTeamIdAndUserId(teamId,userId);
        if(user != null){
            if(user.getAuditStatus().equals("2")){
                throw new RRException("您已申请，请勿重复申请");
            }else if(user.getAuditStatus().equals("1")){
                throw new RRException("您已经加入，无需申请");
            }
        }
        RTeamUser rTeamUser = new RTeamUser();
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        rTeamUser.setId(id);
        rTeamUser.setUserId(userId);
        rTeamUser.setTeamId(teamId);
        rTeamUser.setAuditStatus("2");
        //球员申请加入
        rTeamUser.setApplyType("1");
        mapper.save(rTeamUser);
    }

    @Override
    public int getAuditCount(RTeamUserSearchVo searchVo) {
        return mapper.getAuditCount(searchVo);
    }

    @Override
    public void audit(String id, String auditStatus) {
        RTeamUserDto rTeamUserDto = mapper.get(id);
        RTeamUser rTeamUser = new RTeamUser();
        rTeamUser.setId(id);
        rTeamUser.setAuditStatus(auditStatus);
        mapper.update(rTeamUser);
        GameTeamDto gameTeamDto = gameTeamMP.get(rTeamUserDto.getTeamId());
        if ("1".equals(auditStatus)) {
            UserMessageVo userMessage=  new UserMessageVo();
            userMessage.setUserId(rTeamUserDto.getUserId());
            userMessage.setContent("恭喜您，审核通过，已加入“"+gameTeamDto.getName()+"”球队。");
            userMessageSV.save(userMessage);
        }
        if ("0".equals(auditStatus)) {
            UserMessageVo userMessage=  new UserMessageVo();
            userMessage.setUserId(rTeamUserDto.getUserId());
            userMessage.setContent("抱歉，被拒绝加入“"+gameTeamDto.getName()+"”球队。");
            userMessageSV.save(userMessage);
        }
    }

    @Override
    public List<SysUserDto1> getPlayersByTeamId(String teamId) {
        return mapper.getPlayersByTeamId(teamId);
    }

    @Override
    public void teamApply(String teamId, String userId) {

        RTeamUser user = mapper.getbyTeamIdAndUserId(teamId,userId);
        if(user != null){
            if(user.getAuditStatus().equals("2")){
                throw new RRException("已邀请，无需邀请");
            }else if(user.getAuditStatus().equals("1")){
                throw new RRException("该球员已经加入");
            }
        }
        RTeamUser rTeamUser = new RTeamUser();
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        rTeamUser.setId(id);
        rTeamUser.setUserId(userId);
        rTeamUser.setTeamId(teamId);
        rTeamUser.setAuditStatus("1");
        //球员申请加入
        rTeamUser.setApplyType("2");
        mapper.save(rTeamUser);
    }

}
